package com.beimin.evedata.hibernate;

import java.util.Collection;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Distinct;
import org.hibernate.criterion.Projection;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;

import com.beimin.evedata.model.inv.InvBlueprint;
import com.beimin.evedata.model.inv.InvCategory;
import com.beimin.evedata.model.inv.InvControlTowerResource;
import com.beimin.evedata.model.inv.InvFlag;
import com.beimin.evedata.model.inv.InvGroup;
import com.beimin.evedata.model.inv.InvMarketGroup;
import com.beimin.evedata.model.inv.InvType;
import com.beimin.evedata.repository.InvRepository;

public class HibInvRepository implements InvRepository {
	private final SessionFactory sf;

	public HibInvRepository(SessionFactory sf) {
		this.sf = sf;
	}

	public InvBlueprint getBlueprint(int blueprintID) {
		return (InvBlueprint) sf.getCurrentSession().get(InvBlueprint.class, blueprintID);
	}

	public InvCategory getCategory(int typeID) {
		return (InvCategory) sf.getCurrentSession().get(InvCategory.class, typeID);
	}

	@SuppressWarnings("unchecked")
	public Collection<InvControlTowerResource> getControlTowerResources(int controlTowerTypeID) {
		Session session = sf.getCurrentSession();
		Criteria criteria = session.createCriteria(InvControlTowerResource.class);
		criteria.add(Restrictions.eq("controlTowerTypeID", controlTowerTypeID));
		return criteria.list();
	}

	@SuppressWarnings("unchecked")
	public Collection<Integer> getControlTowerResourcesTypes() {
		Session session = sf.getCurrentSession();
		Criteria criteria = session.createCriteria(InvControlTowerResource.class);
		Projection projection = new Distinct(Projections.groupProperty("resourceTypeID"));
		criteria.setProjection(projection);
		return criteria.list();
	}

	public InvControlTowerResource getControlTowerResource(int controlTowerTypeID, int resourceTypeID) {
		Session session = sf.getCurrentSession();
		Criteria criteria = session.createCriteria(InvControlTowerResource.class);
		criteria.add(Restrictions.eq("controlTowerTypeID", controlTowerTypeID));
		criteria.add(Restrictions.eq("resourceTypeID", resourceTypeID));
		return (InvControlTowerResource) criteria.uniqueResult();
	}

	public InvFlag getFlag(int flagID) {
		return (InvFlag) sf.getCurrentSession().get(InvFlag.class, flagID);
	}

	public InvGroup getGroup(int groupID) {
		return (InvGroup) sf.getCurrentSession().get(InvGroup.class, groupID);
	}

	public InvMarketGroup getMarketGroup(int marketGroupID) {
		return (InvMarketGroup) sf.getCurrentSession().get(InvMarketGroup.class, marketGroupID);
	}

	@SuppressWarnings("unchecked")
	@Override
	public Collection<InvMarketGroup> getMarketGroups(Integer... marketGroupIds) {
		Session session = sf.getCurrentSession();
		Criteria criteria = session.createCriteria(InvMarketGroup.class);
		criteria.add(Restrictions.in("marketGroupID", marketGroupIds));
		return criteria.list();
	}

	@SuppressWarnings("unchecked")
	@Override
	public Collection<InvMarketGroup> getChildMarketGroups(InvMarketGroup parentGroup) {
		Session session = sf.getCurrentSession();
		Criteria criteria = session.createCriteria(InvMarketGroup.class);
		criteria.add(Restrictions.eq("parentMarketGroup", parentGroup));
		return criteria.list();
	}

	public InvType getType(int typeID) {
		return (InvType) sf.getCurrentSession().get(InvType.class, typeID);
	}
}
